﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
namespace Chenxin.Lib.Business.DAL
{
    /// <summary>
    /// 数据访问类AutoCode_IntentionControllerInfo。
    /// </summary>
    public class AutoCode_IntentionControllerInfo
    {
        public AutoCode_IntentionControllerInfo()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from AutoCode_IntentionControllerInfo where RecordId=@RecordId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Business.Entity.AutoCode_IntentionControllerInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into AutoCode_IntentionControllerInfo(");
            strSql.Append("RecordId,PrefixCode,BusinessType,IntentionType,IntentionYear,MaxCode)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@PrefixCode,@BusinessType,@IntentionType,@IntentionYear,@MaxCode)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "PrefixCode", DbType.AnsiString, model.PrefixCode);
            db.AddInParameter(dbCommand, "BusinessType", DbType.AnsiString, model.BusinessType);
            db.AddInParameter(dbCommand, "IntentionType", DbType.AnsiString, model.IntentionType);
            db.AddInParameter(dbCommand, "IntentionYear", DbType.AnsiString, model.IntentionYear);
            db.AddInParameter(dbCommand, "MaxCode", DbType.Int32, model.MaxCode);
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Business.Entity.AutoCode_IntentionControllerInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update AutoCode_IntentionControllerInfo set ");
            strSql.Append("PrefixCode=@PrefixCode,");
            strSql.Append("BusinessType=@BusinessType,");
            strSql.Append("IntentionType=@IntentionType,");
            strSql.Append("IntentionYear=@IntentionYear,");
            strSql.Append("MaxCode=@MaxCode");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "PrefixCode", DbType.AnsiString, model.PrefixCode);
            db.AddInParameter(dbCommand, "BusinessType", DbType.AnsiString, model.BusinessType);
            db.AddInParameter(dbCommand, "IntentionType", DbType.AnsiString, model.IntentionType);
            db.AddInParameter(dbCommand, "IntentionYear", DbType.AnsiString, model.IntentionYear);
            db.AddInParameter(dbCommand, "MaxCode", DbType.Int32, model.MaxCode);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from AutoCode_IntentionControllerInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.AutoCode_IntentionControllerInfo GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,PrefixCode,BusinessType,IntentionType,IntentionYear,MaxCode from AutoCode_IntentionControllerInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Business.Entity.AutoCode_IntentionControllerInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static string GetCode(string strWhere)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Top 1 MaxCode + 1 as MaxCode from AutoCode_IntentionControllerInfo ");
            strSql.Append(strWhere);
            strSql.Append("order by MaxCode desc ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            string retCode = string.Empty;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    retCode = CodeBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return retCode;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.AutoCode_IntentionControllerInfo GetCurrCode(string strWhere)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,PrefixCode,BusinessType,IntentionType,IntentionYear,MaxCode from AutoCode_IntentionControllerInfo ");
            strSql.Append(strWhere);
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            Chenxin.Lib.Business.Entity.AutoCode_IntentionControllerInfo retCode = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    retCode = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return retCode;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,PrefixCode,BusinessType,IntentionType,IntentionYear,MaxCode ");
            strSql.Append(" FROM AutoCode_IntentionControllerInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Business.Entity.AutoCode_IntentionControllerInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,PrefixCode,BusinessType,IntentionType,IntentionYear,MaxCode ");
            strSql.Append(" FROM AutoCode_IntentionControllerInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Business.Entity.AutoCode_IntentionControllerInfo> list = new List<Chenxin.Lib.Business.Entity.AutoCode_IntentionControllerInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Business.Entity.AutoCode_IntentionControllerInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Business.Entity.AutoCode_IntentionControllerInfo model = new Chenxin.Lib.Business.Entity.AutoCode_IntentionControllerInfo();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            model.PrefixCode = dataReader["PrefixCode"].ToString();
            model.BusinessType = dataReader["BusinessType"].ToString();
            model.IntentionType = dataReader["IntentionType"].ToString();
            model.IntentionYear = dataReader["IntentionYear"].ToString();
            ojb = dataReader["MaxCode"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.MaxCode = (int)ojb;
            }
            return model;
        }

        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static string CodeBind(IDataReader dataReader)
        {
            Chenxin.Lib.Business.Entity.AutoCode_IntentionControllerInfo model = new Chenxin.Lib.Business.Entity.AutoCode_IntentionControllerInfo();
            object ojb;
            string retCode = string.Empty;
            ojb = dataReader["MaxCode"];
            if (ojb != null && ojb != DBNull.Value)
            {
                retCode = ojb.ToString();
            }
            return retCode;
        }

        #endregion  成员方法
    }
}

